package com.zhongkai.web.control.book;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zhongkai.model.book.TDjCcdjxx;
import com.zhongkai.model.book.TDjCcdjxxLs;
import com.zhongkai.model.book.TDjCcywcl;
import com.zhongkai.service.book.InsuranceOrService;
import com.zhongkai.tools.CheckWord;

@Controller
public class Insurance_orControll {
	private Logger log=Logger.getLogger(this.getClass());
	private InsuranceOrService insuranceOrService;
	@Resource
	public void setInsuranceOrService(InsuranceOrService insuranceOrService) {
		this.insuranceOrService = insuranceOrService;
	}
	
	/*工作人员提交*/
	@RequestMapping(value="/system/book/insuranceOr_Chec.do")
	public String insuranceOr_Chec(TDjCcdjxx tDjCcdjxx,TDjCcywcl tDjCcywcl,HttpServletRequest request,HttpServletResponse response) throws IOException{
		try {
			PrintWriter out=response.getWriter();
			TDjCcdjxx ccdjxxOld=(TDjCcdjxx)insuranceOrService.selectById(TDjCcdjxx.class,tDjCcdjxx.getCcdjh());
			
			String sbqisnyStr=ccdjxxOld.getSbqsny();
			//申报起始年
			String sbqisnStr=sbqisnyStr.substring(0, 4);
			//转化成整型起始年份
			int sbqsnInt=Integer.valueOf(sbqisnStr);
			//代缴年份起
			String bxdjNfqStr=tDjCcywcl.getBxdjNfq();
			int bxdjNfqInt=Integer.valueOf(bxdjNfqStr);
			//代缴年份止
			String bxdjNfzStr=tDjCcywcl.getBxdjNfz();
			int bxdjNfzInt=Integer.valueOf(bxdjNfzStr);
			//代缴年份止为空则让其等于代缴年份起
			if(bxdjNfzStr==null||"".equals(bxdjNfzStr)){
				bxdjNfzStr=bxdjNfqStr;
				tDjCcywcl.setBxdjNfz(bxdjNfzStr);
			}
			if(bxdjNfqInt>bxdjNfzInt){
				out.write(CheckWord.showMsg("请检查异地交纳年份格式!", "/system/search/unisearch.jsp?module=book/ccsb.jsp",request));
				return null;
			}
			if(sbqsnInt<bxdjNfqInt){
				//提示进入申报
				out.write(CheckWord.showMsg("请先补缴完税再进行操作!", "/system/search/unisearch.jsp?module=book/ccsb.jsp",request));
				return null;
			}
			
			TDjCcdjxxLs tDjCcdjxxLs=new TDjCcdjxxLs();
				BeanUtils.copyProperties(tDjCcdjxx, tDjCcdjxxLs);
				insuranceOrService.saveInsuranceOr_xx(tDjCcdjxx, tDjCcywcl, tDjCcdjxxLs,0,request);
				
				out.write(CheckWord.showMsg("提交审批成功!", "/system/search/unisearch.jsp?module=book/insurance_orform.jsp",request));
				return null;
			
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			PrintWriter out=response.getWriter();
			out.write(CheckWord.showMsg("提交审核时发生异常，请与管理员联系!", "/system/book/insurance_orform.jsp?ccdjh="+tDjCcdjxx.getCcdjh(),request));
		}
		return null;
	}
	
	/*管理员提交与审核*/
	@RequestMapping(value="/system/book/insuranceOr_Apro.do")
	public String insuranceOr_Apro(Integer tDjCcdjxxLsccdjh,TDjCcdjxx tDjCcdjxx,TDjCcywcl tDjCcywcl,HttpServletRequest request,HttpServletResponse response) throws IOException{
		try {
			PrintWriter out=response.getWriter();
			TDjCcdjxx ccdjxxOld=(TDjCcdjxx)insuranceOrService.selectById(TDjCcdjxx.class,tDjCcdjxx.getCcdjh());
			ccdjxxOld.setBz(tDjCcdjxx.getBz());
			
			String sbqisnyStr=ccdjxxOld.getSbqsny();
			//申报起始年
			String sbqisnStr=sbqisnyStr.substring(0, 4);
			//转化成整型起始年份
			int sbqsnInt=Integer.valueOf(sbqisnStr);
			//代缴年份起
			String bxdjNfqStr=tDjCcywcl.getBxdjNfq();
			int bxdjNfqInt=Integer.valueOf(bxdjNfqStr);
			//代缴年份止
			String bxdjNfzStr=tDjCcywcl.getBxdjNfz();
			int bxdjNfzInt=Integer.valueOf(bxdjNfzStr);
			//代缴年份止为空则让其等于代缴年份起
			if(bxdjNfzStr==null||"".equals(bxdjNfzStr)){
				bxdjNfzStr=bxdjNfqStr;
				tDjCcywcl.setBxdjNfz(bxdjNfzStr);
			}
			if(bxdjNfqInt>bxdjNfzInt){
				out.write(CheckWord.showMsg("请检查异地交纳年份格式!", "/system/search/unisearch.jsp?module=book/ccsb.jsp",request));
				return null;
			}
			if(sbqsnInt<bxdjNfqInt){
				//提示进入申报
				out.write(CheckWord.showMsg("请先补缴完税再进行操作!", "/system/search/unisearch.jsp?module=book/ccsb.jsp",request));
				return null;
			}
			
			
			TDjCcdjxxLs tDjCcdjxxLs=new TDjCcdjxxLs();
			insuranceOrService.saveInsuranceOr_xx(tDjCcdjxx, tDjCcywcl, tDjCcdjxxLs,1,request);
			
			
			out.write(CheckWord.showMsg("审核通过!", "/system/search/unisearch.jsp?module=book/insurance_orform.jsp",request));
			return null;
			
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			PrintWriter out=response.getWriter();
			out.write(CheckWord.showMsg("提交批准时发生异常，请与管理员联系!", "/system/book/insurance_orform.jsp?ccdjh="+tDjCcdjxx.getCcdjh(),request));
		}
		return null;
	}
	
	@InitBinder
	public void initBinder(WebDataBinder binder){
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  
	    dateFormat.setLenient(false);  
	    binder.registerCustomEditor(Date.class, new CustomDateEditor(  
	            dateFormat, false)); 
	}

}
